package util.ir;

import java.io.File;
import java.io.IOException;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriter.MaxFieldLength;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

public class IndexMerger {

	/**
	 * @param args
	 */

	public static void mergeIndexes(String source, String output) {

		File INDEXES_DIR = new File(source);
		File INDEX_DIR = new File(output);

		try {
			IndexWriter writer = new IndexWriter(INDEX_DIR,
					new StandardAnalyzer(), true, MaxFieldLength.UNLIMITED);
			writer.setMergeFactor(1000);
			writer.setRAMBufferSizeMB(400);

			Directory indexes[] = new Directory[INDEXES_DIR.list().length];

			for (int i = 0; i < INDEXES_DIR.list().length; i++) {
				System.out.println("Adding: " + INDEXES_DIR.list()[i]);
				indexes[i] = FSDirectory.getDirectory(INDEXES_DIR
						.getAbsolutePath()
						+ "/" + INDEXES_DIR.list()[i]);
			}

			System.out.print("Merging added indexes...");
			writer.addIndexesNoOptimize(indexes);
			System.out.println("done");

			System.out.print("Optimizing index...");
			writer.optimize();
			writer.close();
			System.out.println("done");

		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		String origin= "/media/sata/data/indexes/wikiXML_anchorText/";
		String  target = "/media/sata/data/indexes/unified/";
		
		mergeIndexes(origin,target);
	}

}
